package com.hymjweb.demo.hymj.department_manage.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentCachePojo;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtDepartmentMapper {

    class XtDepartmentMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_department t ");
                WHERE("1=1");
            }}.toString();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("name"));
                String categoryId = DataTypeUtil.toNotNullString(map.get("categoryId"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and t.name like " + DataTypeUtil.quoteAllLikeStr(name);
                }

                if(!DataTypeUtil.isEmptyStr(categoryId)){
                    sql += " and t.category_id = " + DataTypeUtil.quoteStr(name);
                }
            }
            return  sql;
        }


        public String treeQuery(Map param){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_department t ");
                WHERE("1=1");
            }}.toString();

            if(param!=null && !param.isEmpty()){

            }

            return sql;
        }

        public String insert(XtDepartmentPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("xt_department");
                VALUES("id","#{id}");
                VALUES("pid","#{pid}");
                VALUES("name","#{name}");
                VALUES("phone","#{phone}");
                VALUES("address","#{address}");
                VALUES("leader_id","#{leaderId}");
                VALUES("company_id","#{companyId}");
                VALUES("city_id","#{cityId}");
                VALUES("category_id","#{categoryId}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtDepartmentPojo pojo) {
            String sql= new SQL() {{
                UPDATE("xt_department xd");
                SET("xd.pid = #{pid}");
                SET("xd.name = #{name}");
                SET("xd.phone = #{phone}");
                SET("xd.address = #{address}");
                SET("xd.leader_id = #{leaderId}");
                SET("xd.company_id = #{companyId}");
                SET("xd.city_id = #{cityId}");
                SET("xd.category_id = #{categoryId}");
                SET("xd.comment = #{comment}");
                SET("xd.update_time = #{updateTime}");
                SET("xd.update_user_id = #{updateUserId}");
                WHERE("xd.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_department");
                WHERE("id = #{id}");
            }}.toString();
            return sql;
        }

        public String findCacheAll() {
            return new SQL() {{
                SELECT("t.*");
                FROM("xt_department t ");
            }}.toString();
        }

        public String findUK(String name,String exceptId) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_department t where t.name = #{name}");
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }

        public String findByLeaderId(String leaderId) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_department t where t.leader_id = #{leaderId}");
            }}.toString();

            return  sql;
        }

        public String getDepartmentByUserId(String userId) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_department t where t.id in (select xu.department_id from xt_user xu where xu.id= #{userId})");
            }}.toString();

            return  sql;
        }

    }

    @InsertProvider(type = XtDepartmentMapperProvider.class, method = "insert")
    int insert(XtDepartmentPojo pojo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @UpdateProvider(type = XtDepartmentMapperProvider.class, method = "update")
    int update(XtDepartmentPojo obj);

    @DeleteProvider(type = XtDepartmentMapperProvider.class, method = "delete")
    int delete(String id);

    @Results(id="xtDepartmentCacheMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "findCacheAll")
    List<XtDepartmentCachePojo> findCacheAll();

    @ResultMap(value="xtDepartmentCacheMapId")
    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "findUK")
    List<XtDepartmentCachePojo> findUK(String name,String exceptId);

    @Results(id = "xtDepartmentMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "name", property = "name"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "address", property = "address"),
            @Result(column = "leader_id", property = "leaderId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "comment", property = "comment"),
    })
    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "findAll")
    List<XtDepartmentPojo> findAll(Map map);

    @ResultMap(value="xtDepartmentMapId")
    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "findByLeaderId")
    List<XtDepartmentPojo> findByLeaderId(String leaderId) ;


    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "getDepartmentByUserId")
    @ResultMap(value="xtDepartmentMapId")
    XtDepartmentPojo getDepartmentByUserId(String userId);

    @ResultMap(value="xtDepartmentMapId")
    @SelectProvider(type = XtDepartmentMapperProvider.class, method = "treeQuery")
    List<XtDepartmentPojo> treeQuery(Map param);

}
